winsafe\shell\com_interfaces/
ifiledialog.rs

1#![allow(non_camel_case_types, non_snake_case)]
2
3use crate::co;
4use crate::decl::*;
5use crate::kernel::privs::*;
6use crate::ole::privs::*;
7use crate::prelude::*;
8use crate::shell::vts::*;
9
10com_interface! { IFileDialog: "42f85136-db7e-439c-85f1-e4075d135fc8";
11	/// [`IFileDialog`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifiledialog)
12	/// COM interface.
13	///
14	/// Automatically calls
15	/// [`Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
16	/// when the object goes out of scope.
17}
18
19impl shell_IModalWindow for IFileDialog {}
20impl shell_IFileDialog for IFileDialog {}
21
22/// This trait is enabled with the `shell` feature, and provides methods for
23/// [`IFileDialog`](crate::IFileDialog).
24///
25/// Prefer importing this trait through the prelude:
26///
27/// ```no_run
28/// use winsafe::prelude::*;
29/// ```
30pub trait shell_IFileDialog: shell_IModalWindow {
31	/// [`IFileDialog::AddPlace`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-addplace)
32	/// method.
33	fn AddPlace(&self, si: &impl shell_IShellItem, fdap: co::FDAP) -> HrResult<()> {
34		ok_to_hrresult(unsafe {
35			(vt::<IFileDialogVT>(self).AddPlace)(self.ptr(), si.ptr(), fdap.raw())
36		})
37	}
38
39	/// [`IFileDialog::Advise`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-advise)
40	/// method.
41	fn Advise(&self, fde: &IFileDialogEvents) -> HrResult<u32> {
42		let mut cookie = 0u32;
43		ok_to_hrresult(unsafe {
44			(vt::<IFileDialogVT>(self).Advise)(self.ptr(), fde.ptr(), &mut cookie)
45		})
46		.map(|_| cookie)
47	}
48
49	fn_com_noparm! { ClearClientData: IFileDialogVT;
50		/// [`IFileDialog::ClearClientData`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-clearclientdata)
51		/// method.
52	}
53
54	/// [`IFileDialog::Close`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-close)
55	/// method.
56	fn Close(&self, hr: co::ERROR) -> HrResult<()> {
57		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).Close)(self.ptr(), hr.raw() as _) })
58	}
59
60	fn_com_interface_get! { GetCurrentSelection: IFileDialogVT => IShellItem;
61		/// [`IFileDialog::GetCurrentSelection`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getcurrentselection)
62		/// method.
63	}
64
65	/// [`IFileDialog::GetFileName`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getfilename)
66	/// method.
67	#[must_use]
68	fn GetFileName(&self) -> HrResult<String> {
69		let mut pstr = std::ptr::null_mut::<u16>();
70		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).GetFileName)(self.ptr(), &mut pstr) })
71			.map(|_| htaskmem_ptr_to_str(pstr))
72	}
73
74	/// [`IFileDialog::GetFileTypeIndex`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getfiletypeindex)
75	/// method.
76	#[must_use]
77	fn GetFileTypeIndex(&self) -> HrResult<u32> {
78		let mut index = 0u32;
79		ok_to_hrresult(unsafe {
80			(vt::<IFileDialogVT>(self).GetFileTypeIndex)(self.ptr(), &mut index)
81		})
82		.map(|_| index)
83	}
84
85	fn_com_interface_get! { GetFolder: IFileDialogVT => IShellItem;
86		/// [`IFileDialog::GetFolder`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getfolder)
87		/// method.
88	}
89
90	/// [`IFileDialog::GetOptions`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getoptions)
91	/// method.
92	#[must_use]
93	fn GetOptions(&self) -> HrResult<co::FOS> {
94		let mut opts = co::FOS::default();
95		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).GetOptions)(self.ptr(), opts.as_mut()) })
96			.map(|_| opts)
97	}
98
99	fn_com_interface_get! { GetResult: IFileDialogVT => IShellItem;
100		/// [`IFileDialog::GetResult`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-getresult)
101		/// method.
102		///
103		/// If you chose a single file, this is the method to retrieve its path.
104	}
105
106	/// [`IFileDialog::SetClientGuid`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setclientguid)
107	/// method.
108	fn SetClientGuid(&self, guid: &GUID) -> HrResult<()> {
109		ok_to_hrresult(unsafe {
110			(vt::<IFileDialogVT>(self).SetClientGuid)(self.ptr(), pcvoid(guid))
111		})
112	}
113
114	/// [`IFileDialog::SetDefaultExtension`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setdefaultextension)
115	/// method.
116	fn SetDefaultExtension(&self, default_extension: &str) -> HrResult<()> {
117		ok_to_hrresult(unsafe {
118			(vt::<IFileDialogVT>(self).SetDefaultExtension)(
119				self.ptr(),
120				WString::from_str(default_extension).as_ptr(),
121			)
122		})
123	}
124
125	/// [`IFileDialog::SetDefaultFolder`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setdefaultfolder)
126	/// method.
127	fn SetDefaultFolder(&self, si: &impl shell_IShellItem) -> HrResult<()> {
128		ok_to_hrresult(unsafe {
129			(vt::<IFileDialogVT>(self).SetDefaultFolder)(self.ptr(), si.ptr())
130		})
131	}
132
133	/// [`IFileDialog::SetFileName`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfilename)
134	/// method.
135	fn SetFileName(&self, name: &str) -> HrResult<()> {
136		ok_to_hrresult(unsafe {
137			(vt::<IFileDialogVT>(self).SetFileName)(self.ptr(), WString::from_str(name).as_ptr())
138		})
139	}
140
141	/// [`IFileDialog::SetFileNameLabel`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfilenamelabel)
142	/// method.
143	fn SetFileNameLabel(&self, label: &str) -> HrResult<()> {
144		ok_to_hrresult(unsafe {
145			(vt::<IFileDialogVT>(self).SetFileNameLabel)(
146				self.ptr(),
147				WString::from_str(label).as_ptr(),
148			)
149		})
150	}
151
152	/// [`IFileDialog::SetFileTypeIndex`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfiletypeindex)
153	/// method.
154	///
155	/// **Note:** The index is one-based.
156	fn SetFileTypeIndex(&self, index: u32) -> HrResult<()> {
157		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).SetFileTypeIndex)(self.ptr(), index) })
158	}
159
160	/// [`IFileDialog::SetFileTypes`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfiletypes)
161	/// method.
162	///
163	/// # Examples
164	///
165	/// ```no_run
166	/// use winsafe::{self as w, prelude::*};
167	///
168	/// let file_dlg: w::IFileDialog; // initialized somewhere
169	/// # let file_dlg = unsafe { w::IFileDialog::null() };
170	///
171	/// file_dlg.SetFileTypes(&[
172	///     ("Documents", "*.docx;*.txt"),
173	///     ("Images", "*.jpg;*.png;*.bmp"),
174	///     ("All files", "*.*"),
175	/// ])?;
176	/// # w::HrResult::Ok(())
177	/// ```
178	fn SetFileTypes<S: AsRef<str>>(&self, filter_spec: &[(S, S)]) -> HrResult<()> {
179		let (mut strs_buf, mut com_dlgs): (Vec<_>, Vec<_>) = filter_spec
180			.iter()
181			.map(|(name, spec)| {
182				let wname = WString::from_str(name.as_ref());
183				let wspec = WString::from_str(spec.as_ref());
184				((wname, wspec), COMDLG_FILTERSPEC::default())
185			})
186			.unzip();
187
188		strs_buf
189			.iter_mut()
190			.zip(com_dlgs.iter_mut())
191			.for_each(|((name, spec), com_dlg)| {
192				com_dlg.set_pszName(Some(name));
193				com_dlg.set_pszSpec(Some(spec));
194			});
195
196		ok_to_hrresult(unsafe {
197			(vt::<IFileDialogVT>(self).SetFileTypes)(
198				self.ptr(),
199				filter_spec.len() as _,
200				com_dlgs.as_ptr() as _,
201			)
202		})
203	}
204
205	/// [`IFileDialog::SetFilter`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfilter)
206	/// method.
207	fn SetFilter(&self, filter: &IShellItemFilter) -> HrResult<()> {
208		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).SetFilter)(self.ptr(), filter.ptr()) })
209	}
210
211	/// [`IFileDialog::SetFolder`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfolder)
212	/// method.
213	fn SetFolder(&self, si: &impl shell_IShellItem) -> HrResult<()> {
214		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).SetFolder)(self.ptr(), si.ptr()) })
215	}
216
217	/// [`IFileDialog::SetOkButtonLabel`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setokbuttonlabel)
218	/// method.
219	fn SetOkButtonLabel(&self, text: &str) -> HrResult<()> {
220		ok_to_hrresult(unsafe {
221			(vt::<IFileDialogVT>(self).SetOkButtonLabel)(
222				self.ptr(),
223				WString::from_str(text).as_ptr(),
224			)
225		})
226	}
227
228	/// [`IFileDialog::SetOptions`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setoptions)
229	/// method.
230	fn SetOptions(&self, opts: co::FOS) -> HrResult<()> {
231		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).SetOptions)(self.ptr(), opts.raw()) })
232	}
233
234	/// [`IFileDialog::SetTitle`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-settitle)
235	/// method.
236	fn SetTitle(&self, text: &str) -> HrResult<()> {
237		ok_to_hrresult(unsafe {
238			(vt::<IFileDialogVT>(self).SetTitle)(self.ptr(), WString::from_str(text).as_ptr())
239		})
240	}
241
242	/// [`IFileDialog::Unadvise`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-unadvise)
243	/// method.
244	fn Unadvise(&self, cookie: u32) -> HrResult<()> {
245		ok_to_hrresult(unsafe { (vt::<IFileDialogVT>(self).Unadvise)(self.ptr(), cookie) })
246	}
247}